from django.db.models import Q
from django.shortcuts import render
from django.views.generic import ListView
from rest_framework import serializers
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework.views import APIView
from unicodedata import category

from course import models, courseser
from course.courseser import CourseSer
from course.models import CourseModel
from home.homeser import Category
from home.models import CategoryModel


# Create your views here.
# 方向一级分类父级
class DirectionView(APIView):
    def get(self, request):
        data =CategoryModel.objects.filter(is_delete=False,parent=None).order_by('order')
        ser = Category(data,many=True)
        return Response({"msg":"ok","code":200,"data":ser.data})
# 分类 二级分类
class CateView(APIView):
    def get(self, request):
        id = int(request.query_params.get('pid'))
        if id !=0 :
            data = CategoryModel.objects.filter(is_delete=False,parent=id).order_by('order')
        else:
            data = CategoryModel.objects.exclude(is_delete=False,parent=None).order_by('order')
        ser = Category(data,many=True)
        return Response({"msg": "ok", "code": 200, "data": ser.data})


# 分页
class CoursePage(PageNumberPagination):
    page_size = 2
    page_size_query_param = 'size'
    page_query_param = 'page'

# 课程列表 二级下的 三级分类
class Course(ListAPIView):
    # 结果集 根据条件获取到结果集
    queryset = models.CourseModel.objects.filter(is_delete=False)
    # 对获取到的数据进行序列化
    serializer_class = courseser.CourseSer
    # 课程要分页 根据条件进行分页
    pagination_class = CoursePage
    def get_queryset(self):
        query = Q()
        # 获取到分类的id或者方向的id
        cateid=int(self.request.query_params.get('cate'))# 获取分类id   二级分类子分类下还有分类
        directionid = int(self.request.query_params.get('direction'))#获取方向的id    一级分类父级分类
        level =self.request.query_params.get('level',"")#
        ordering = self.request.query_params.get('ordering',"")
        # 如果二级分类不等于0，说明要查找二级分类下的分类
        # 根据前端路径上传来的 cate =.. 截取 获取到id 外键在根据id 找到相对应的数据
        if cateid !=0:
            # 获取到一个结果集，，有两种写法
            query = query&Q(category = cateid)
            # query &= Q(category = cateid)
        #     如果方向不等于0 也就是说主分类 一级分类不等于0，要查找父级分类下的子分类，
        # 查找二级分类  根据id查找所有的二级分类
        if directionid !=0:
            data = CategoryModel.objects.filter(is_delete=False,parent=directionid)
            catelist =[]
            # 循环data,获取到的二级分类，将每个分类的传进列表
            for i in data:
                catelist.append(i.id) #[1,2,3,]
                # 如果外键的那个数字 在这个列表里 筛选出来
                query &= Q(category__in=catelist)
        if level:
            query &=Q(level =level)
        if ordering:
            return self.queryset.filter(query).order_by(ordering)
        else:
            return self.queryset.filter(query)
# 课程跳转回显数据 有老师信息 的视图
class DetailView(APIView):
    def get(self, request):
        # 获取到课程的id
        id =request.query_params.get('id')
        # 根据三级分类获取到教师的信息
        data = CourseModel.objects.filter(is_delete=False,id=id).first()
        # 进行序列化 课程的序列化
        ser = CourseSer(data)
        return Response({"msg":"ok","code":200,"data":ser.data})

# 验证认证的类
from utils.authentication import MyAuthentication
class Test(APIView):
    # 继承authentication 里面的MyAuthentication 类第一-->authentication
    authentication_classes =[MyAuthentication]
    def get(self, request):
        # request-->接收来自authentitation 接收到返回响应“验证的类成功”
        return Response("验证的类成功")


